package com.hy.mydesktop.client.component.factory;

import java.util.List;

import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.hy.mydesktop.client.mvc.core.event.manager.ComponentTrrigerTemplateUtil;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

public class GxtComponentMetaNodeModelChildrenItemsFactory {

	public static Component configureChildrenItems(Component component,
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		ContentPanel panel=null;
		if (component instanceof ContentPanel) {
			
			panel =configureContentPanelChildrenItems((ContentPanel)component,gxtComponentMetaNodeModel);
			return panel;
		}
		
		MenuItem menuItem  = null;
		if (component instanceof MenuItem) {

			menuItem =configureContentPanelChildrenItems((MenuItem)component,gxtComponentMetaNodeModel);
			return menuItem;
		}
		


		return null;
		//throw new RuntimeException("vvvvvvvvvvvvvv");
			
		
	}

	private static MenuItem configureContentPanelChildrenItems(
			final MenuItem menuItem,
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		//进行个性化配置
		//ItemOfAssociateToWindowFactory.configureChildrenItems(menuItem,gxtComponentMetaNodeModel);
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS)!=null){
			String childrenItemsType=gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS_TYPE);

			if (childrenItemsType==null||childrenItemsType.equals("component")) {
				List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
				 if (list.size()>0) {
					 Menu menu=new Menu();
					 for(int i=0;i<list.size();i++)
					 {
						 MenuItem subItem=MenuItemFactory.createItemOfAssociateToWindow(list.get(i));
						 menu.add(subItem);
					 }	
					 menuItem.setSubMenu(menu);
				}
			} else if(childrenItemsType.equals("data")){
				List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
				menuItem.setData(ComponentMetaDataConstants.CHILDREN_ITEMS, list);
				for (ItemOfAssociateToWindow itemOfAssociateToWindow : list) {
				}
				//为控件注册事件
				final ItemOfAssociateToWindow item = list.get(0);
				if (item!=null&&item.getRegisterEventType()!=null) {
					SelectionListener<MenuEvent> shortcutListener = new SelectionListener<MenuEvent>() {
					      @Override
					      public void componentSelected(MenuEvent ce) {
					    	  AppEvent appEvent =ComponentTrrigerTemplateUtil.createAppEvent(menuItem, item);
					    	  if (appEvent!=null) {								
					    		  Dispatcher.forwardEvent(appEvent);
					    	  }else {
					    		  MyLoggerUtil.printWarn(DesktopMenuItemFactory.class,
					    				  menuItem.getClass().getName()+"【id="
											 +menuItem.getData(ComponentMetaDataConstants.COMPONENT_ID)
											 +"】，没有 注册需要触发的AppEvents"+
											 "事件;");
					    	  }
							 
					      }};
					 menuItem.addSelectionListener(shortcutListener);
				}		
				
			}else {
				MyLoggerUtil.printError(DesktopMenuItemFactory.class, menuItem.getClass().getName()+
						"的childrenItemsType，既不是component，也不是data，"
						+"实际得到的是"+childrenItemsType);
			}
			
		}
		
		return menuItem;
	}

	private static ContentPanel configureContentPanelChildrenItems(
			ContentPanel panel,
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		//进行个性化配置
		//ItemOfAssociateToWindowFactory.configureChildrenItems(menuItem,gxtComponentMetaNodeModel);
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS)!=null){
			String childrenItemsType=gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS_TYPE);

			if (childrenItemsType==null||childrenItemsType.equals("component")
					||childrenItemsType.equals(ComponentMetaDataConstants.COMPONENT_CHILDREN_ITEMS_TYPE)) {
				List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
				if (list!=null) {
					for (ItemOfAssociateToWindow itemOfAssociateToWindow : list) {
						 GxtComponentMetaNodeModel subGxtComponentMetaNodeModel=
							 new GxtComponentMetaNodeModel(itemOfAssociateToWindow.getProperties());
						 Component subComponent=GxtComponentFactory.ItemGxtComponent(subGxtComponentMetaNodeModel);
						 panel.add(subComponent);
					}
					 
				}
			}else if (childrenItemsType.equals(ComponentMetaDataConstants.TWOCOLUMN_PANELINFORM_CHILDREN_ITEMS_TYPE)) {
				List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
				ContentPanel left=null;
				ContentPanel right=null;
				
				if (list!=null) {
				    left=new ContentPanel();
				    left.setHeaderVisible(false);
				    left.setLayout(new FormLayout());
				    right=new ContentPanel();
				    right.setHeaderVisible(false);
				    right.setLayout(new FormLayout());
				    for (int i = 0; i < list.size(); i++) {
				    	GxtComponentMetaNodeModel subGxtComponentMetaNodeModel=
							 new GxtComponentMetaNodeModel(list.get(i).getProperties());
						Component subComponent=GxtComponentFactory.ItemGxtComponent(subGxtComponentMetaNodeModel);
						if (i%2==0) {
							left.add(subComponent);
						} else {
							right.add(subComponent);
						}
					}
				}
				panel.setLayout(new ColumnLayout());
				panel.add(left, new ColumnData(.5));
				panel.add(right, new ColumnData(.5));

			} else if(childrenItemsType.equals("data")){
				/*List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
				menuItem.setData(ComponentMetaDataConstants.CHILDREN_ITEMS, list);
				for (ItemOfAssociateToWindow itemOfAssociateToWindow : list) {
				}
				//为控件注册事件
				final ItemOfAssociateToWindow item = list.get(0);
				if (item!=null&&item.getRegisterEventType()!=null) {
					SelectionListener<MenuEvent> shortcutListener = new SelectionListener<MenuEvent>() {
					      @Override
					      public void componentSelected(MenuEvent ce) {
					    	  AppEvent appEvent =ComponentTrrigerTemplateUtil.createAppEvent(menuItem, item);
					    	  if (appEvent!=null) {								
					    		  Dispatcher.forwardEvent(appEvent);
					    	  }else {
					    		  MyLoggerUtil.printWarn(DesktopMenuItemFactory.class,
					    				  menuItem.getClass().getName()+"【id="
											 +menuItem.getData(ComponentMetaDataConstants.COMPONENT_ID)
											 +"】，没有 注册需要触发的AppEvents"+
											 "事件;");
					    	  }
							 
					      }};*/
					 //menuItem.addSelectionListener(shortcutListener);
				}		
				
			}else {
				/*MyLoggerUtil.printError(DesktopMenuItemFactory.class, menuItem.getClass().getName()+
						"的childrenItemsType，既不是component，也不是data，"
						+"实际得到的是"+childrenItemsType);*/
			}
		
		return panel;
		
	}
	
	
	public static Component configureBaseComponentId(Component component,ItemOfAssociateToWindow gxtComponentMetaNodeModel){


		//设置基础控件的Id
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID)!=null){
			String id =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID);
			
			//component.setId(id.replace('.', '_'));
			component.setData(ComponentMetaDataConstants.ID,id);
			component.setData(ComponentMetaDataConstants.COMPONENT_ID,id);
		
		}else {
			
			if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID)!=null){
				//设置基础控件的Id
				String id =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID);
				;
				//component.setId(id.replace('.', '_'));
				component.setData(ComponentMetaDataConstants.ID,id);
				component.setData(ComponentMetaDataConstants.COMPONENT_ID,id);
				
			}else {
				MyLoggerUtil.printDebug(ItemOfAssociateToWindowFactory.class, "没有为当前控件"+component.getClass().getName()+"，设置控件ID");
			}
		}
		
		return component;
	}

}
